package com.ruicar.afs.cloud.risk.tortoise.processor.impl;

import com.ruicar.afs.cloud.common.core.util.SpringContextHolder;
import com.ruicar.afs.cloud.risk.api.dto.tortoise.request.TortoisePrevApproveData;
import com.ruicar.afs.cloud.risk.api.enums.tortoise.GwtTortoiseScene;
import com.ruicar.afs.cloud.risk.tortoise.components.GwtTortoiseStrRedis;
import com.ruicar.afs.cloud.risk.tortoise.dto.notice.GwtTortoiseReq;
import com.ruicar.afs.cloud.risk.tortoise.dto.response.GwtTortoiseResponse;
import com.ruicar.afs.cloud.risk.tortoise.dto.response.prev.GwtPrevApproveData;
import com.ruicar.afs.cloud.risk.tortoise.enums.GwtTortoiseNoticeClientUri;
import com.ruicar.afs.cloud.risk.tortoise.processor.GwtTortoiseQueryAndNoticeProcessor;
import com.ruicar.afs.cloud.risk.tortoise.service.AfsCreditTortoiseRecordService;
import com.ruicar.afs.cloud.risk.tortoise.util.RefDataUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * @Description
 * @Author Peng Jinbo
 * @Date 2020/07/31 20:27
 */
@Component
@AllArgsConstructor
public class GwtTortoisePrevApproveProcessorImpl implements GwtTortoiseQueryAndNoticeProcessor {

    /**
     * Afs credit tortoise record service
     */
    private AfsCreditTortoiseRecordService afsCreditTortoiseRecordService;

    /**
     * Gwt tortoise str redis
     */
    private final GwtTortoiseStrRedis gwtTortoiseStrRedis;

    /**
     * Author Peng Jinbo
     * Belong scene set
     *
     * @return the set
     */
    @Override
    public Set<GwtTortoiseScene> belongScene() {
        return new HashSet(){{add(GwtTortoiseScene.PREV_APPROVE);}};
    }

    /**
     * Author Peng Jinbo
     * Query gwt tortoise response
     *
     * @param notice notice
     * @return the gwt tortoise response
     */
    @Override
    public GwtTortoiseResponse query(GwtTortoiseReq notice) {
        SpringContextHolder.getBean(GwtTortoiseApplyProcessorImpl.class).setDefaultExpireWhenNotExistsData(notice.getCaMainId(),notice.getSceneCode());
        TortoisePrevApproveData prevApproveData = gwtTortoiseStrRedis.assertGet(notice, TortoisePrevApproveData.class);
        GwtPrevApproveData prevData = GwtPrevApproveData.builder()
                .mainId(prevApproveData.getMainId())
                .applyNo(prevApproveData.getApplyNo())
                .businessType(prevApproveData.getMainInfo().getBusinessType())
                .sysCallLogId(prevApproveData.getSerialNumber())
                .sellUser(prevApproveData.getMainInfo().getSellStaffName())
                .sellUserPhone(prevApproveData.getMainInfo().getSellStaffPhone())
                .dealerName(prevApproveData.getMainInfo().getDealerName())
                .dealerProvinceDesc(prevApproveData.getDealerInfo().getProvinceName())
                .dealerCityDesc(prevApproveData.getDealerInfo().getCityName())
                .dealerRegisterDate(prevApproveData.getDealerInfo().getStartWorkDate())
                .customerName(prevApproveData.getCustomerInfo().getName())
                .customerPhone(prevApproveData.getCustomerInfo().getPhone())
                .customerBirthday(prevApproveData.getCustomerInfo().getBirthday())
                .customerCardEndDate(prevApproveData.getCustomerInfo().getIdCardEndDate())
                .customerCardId(prevApproveData.getCustomerInfo().getIdCard())
                .customerCardType(prevApproveData.getCustomerInfo().getCardType())
                .customerSex(prevApproveData.getCustomerInfo().getSex())
                .build();
        return GwtTortoiseResponse.successData(RefDataUtil.addRefData(prevData));
    }

    /**
     * Author Peng Jinbo
     * Notice gwt tortoise response
     *
     * @param notice notice
     * @return the gwt tortoise response
     */
    @Override
    public GwtTortoiseResponse notice(GwtTortoiseReq notice) {
        afsCreditTortoiseRecordService.noticeToClient(notice, GwtTortoiseNoticeClientUri.NOTICE_CLIENT_URI_PREV);
        return GwtTortoiseResponse.success();
    }
}
